# Set working directory. Please set your own
setwd("~/Desktop/PSRM Replication")

rm(list = ls())
library(foreign)
library(ggplot2)
library(readstata13)
library(reshape2) 



theme_base2 <- 
  theme_minimal(base_size=20)  + 
  theme(legend.position=c(0.15, .9),  legend.key.size = unit(1,"line"),
        axis.text=element_text(size=20),axis.title.x=element_text(size=20),axis.title.y=element_text(size=20),
        plot.title = element_text(size=20, hjust= 0.5))

fran <- read.dta13("ttest_results.dta")


label= c("High income", "Parties only votes", "Pres. Approval", "Moved", "Ubran", "Northeast","North-Central","South","West","Trust gov.","Ext efficacy","Vote say","Int.efficacy","Politics complicated","Voting matters","Government Spending")
sorti2<-data.frame(fran,label)
newdata <- sorti2[order(sorti2$p_value),]

ggplot(newdata, aes(sample = p_value)) + stat_qq(distribution = stats::qunif)+  geom_abline(linetype = "dashed")  + 
  geom_text(label=newdata$label, stat="qq", distribution = stats::qunif,hjust=.5, vjust=-1.3, size=2.5) +
  geom_vline(xintercept = 0.05, linetype = "dashed", color = "red3") + 
  geom_vline(xintercept = 0.1, linetype = "dashed", color = "blue") +
  geom_hline(yintercept = 0.05, linetype = "dashed", color = "red3") + 
  geom_hline(yintercept = 0.1, linetype = "dashed", color = "blue") +   
  theme_base2 + ylab("p-values") + xlab("Uniform Distribution") +ylim(0,1) +xlim(0,1) + ggtitle("Presidential\n vs Midterm Eligibles")
ggsave("Graphs/unifbalance.pdf", width = 22, height = 22, units = "cm")  
